package com.example.demo.controller;

import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequestMapping("/demo")
@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("/dict/list")
    public Map loadDictList(){
        Map result = new HashMap();
        try{
            Map param=new HashMap();
            List<Map> dictList = demoService.loadDictList(param);


            result.put("success",true);
            result.put("datalist",dictList);
        }catch (Exception e){
            result.put("success",false);
            result.put("message","查询错误");
            e.printStackTrace();
        }
        return result;
    }


    //测试数据
    @RequestMapping("/mock")
    public Map demoMock(){
        Map result = new HashMap();
        try{

            Random random = new Random();
            for(int i=1;i<5000;i++){
                String tjrq="";
                int k=i%10;
                if(k==0){
                    k=1;
                }
                if(k<10){
                    tjrq="2022-12-0"+k;
                }else{
                    tjrq="2022-12-"+k;
                }

                Map pm=new HashMap();
                pm.put("tjrq",tjrq);
                pm.put("tjlb","LB00"+i%3);
                pm.put("tjnum",random.nextInt(1000));
                demoService.savadata(pm);
            }
            result.put("success",true);
        }catch (Exception e){
            result.put("success",false);
            result.put("message","查询错误");
            e.printStackTrace();
        }
        return result;
    }




    @RequestMapping("/exam1")
    @ResponseBody
    public Map exam1(){
        Map result = new HashMap();
        try{
            Map<String,String> param=new HashMap();
            param.put("startDate","2022-12-01");
            param.put("endDate","2022-12-09");

            List<Map> resultD = demoService.examtest1(param);
            result.put("success",true);
            result.put("datalist",resultD);
        }catch (Exception e){
            result.put("success",false);
            result.put("message","查询错误");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/exam2")
    @ResponseBody
    public Map exam2(){
        Map result = new HashMap();
        try{
            Map<String,String> param=new HashMap();
            param.put("startDate","2022-12-01");
            param.put("endDate","2022-12-09");

            List<LinkedHashMap<String,String>> resultD = demoService.examtest2(param);
            result.put("success",true);
            result.put("datalist",resultD);
        }catch (Exception e){
            result.put("success",false);
            result.put("message","查询错误");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/exam3")
    @ResponseBody
    public Map exam3(){
        Map result = new HashMap();
        try{
            Map<String,String> param=new HashMap();
            param.put("startDate","2022-12-01");
            param.put("endDate","2022-12-09");

            LinkedHashMap<String, Object> resultD = demoService.examtest3(param);
            result.put("success",true);
            result.put("datalist",resultD);
        }catch (Exception e){
            result.put("success",false);
            result.put("message","查询错误");
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("/exam4")
    @ResponseBody
    public Map exam4(){
        Map result = new HashMap();
        try{
            Map<String,String> param=new HashMap();
            param.put("startDate","2022-12-01");
            param.put("endDate","2022-12-09");

            List<Map<String,String>> resultD = demoService.examtest4(param);
            result.put("success",true);
            result.put("datalist",resultD);
        }catch (Exception e){
            result.put("success",false);
            result.put("message","查询错误");
            e.printStackTrace();
        }
        return result;
    }
}
